package com.mapapp.flutter_mapapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.bean.StepFloorBean;
import com.mapapp.flutter_mapapp.bean.StepFloorProcessBean;
import com.mapapp.flutter_mapapp.widget.CustomizedRoundProgressBar;

import java.util.ArrayList;
import java.util.List;

public class StepDetailAdapter extends RecyclerView.Adapter<StepDetailAdapter.StepGrossViewHolder>{

    private ArrayList<StepFloorProcessBean> mProcess;
    private Context mContext;

    public StepDetailAdapter(ArrayList<StepFloorProcessBean> process , Context content){
        this.mProcess = process;
        this.mContext = content;
    }

    public void notify(ArrayList<StepFloorProcessBean> process){
        this.mProcess = process;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public StepGrossViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dialog_step_detail, parent, false);
        StepGrossViewHolder holder = new StepGrossViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull StepGrossViewHolder holder, final int position) {
        String[] colors = mContext.getResources().getStringArray(R.array.stepcolor);
        StepFloorProcessBean bean = mProcess.get(position);
        holder.name.setText(bean.getName());
        if(position < 5){
            holder.bar.setGradualColor(Color.parseColor(colors[position]));
        }else{
            holder.bar.setGradualColor(Color.parseColor(colors[5]));
        }
        holder.bar.setCurrentCount((float) bean.getPercent()*100);
        if(bean.getPercent() == 1){
            holder.state.setText("已完成");
        }else{
            holder.state.setText("未完成");
        }
        if(position == mProcess.size() - 1){
            holder.line.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        if (mProcess == null) {
            return 0;
        }
        return mProcess.size();
    }

    static class StepGrossViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView state;
        private CustomizedRoundProgressBar bar;
        private View line;

        public StepGrossViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_dialog_item_step_name);
            state = itemView.findViewById(R.id.tv_dialog_item_step_state);
            bar = itemView.findViewById(R.id.crp_dialog_item_step_percent);
            line = itemView.findViewById(R.id.view_dialog_item_step_line);
        }
    }

}
